import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { CoursesModule } from './courses/courses.module';
import { EpisodesModule } from './episodes/episodes.module';
import { MulterModule } from '@nestjs/platform-express';
import { CommonModule } from '@app/common';
import { ConfigService } from '@nestjs/config';
import { EnvEnum } from '@app/common/enum/envEnum';
import { OssType } from '@app/common/types/oss';
import { ServeStaticModule } from '@nestjs/serve-static';
import { CategoriesModule } from './categories/categories.module';
import { join } from 'path';

const MAO = require('multer-aliyun-oss');

@Module({
  imports: [
    CommonModule,
    MulterModule.registerAsync({
      useFactory(configService: ConfigService) {
        if (configService.get('IS_OSS') === 'true') {
          return {
            storage: MAO({
              config: configService.get<OssType>(EnvEnum.ossOptions),
            }),
          };
        } else {
          return {
            dest: 'uploads',
          };
        }
      },
      inject: [ConfigService],
    }),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client-admin'),
      serveRoot: '/admin',
    }),
    UsersModule,
    CoursesModule,
    EpisodesModule,
    CategoriesModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
